import React from 'react';
import { Form } from 'antd';
import { ParamType, ValueType } from '@/component/param-setting/enum';
import WSelect from '@/component/param-setting/widgets/w-select';
import WInput from '@/component/param-setting/widgets/w-input';
import WSwitch from '@/component/param-setting/widgets/w-switch';
import WSetting from '@/component/param-setting/widgets/w-setting';

const getLabel = (obj = {}, value: string) => {
  return Object.entries(obj).find(([key, val]) => val === value)?.[0];
};

const widgetMap: any = {
  paramType: WSelect,
  valueType: WSelect,
  optional: WSwitch,
  dataValueOptions: WSetting,
};

export default function EditableCell(props: any) {
  const {
    editing,
    dataIndex,
    title,
    inputType,
    record,
    index,
    children,
    onSaveValue,
    hidden,
    hasOptional,
    ...restProps
  } = props;
  let value = children;
  const W: any = widgetMap[dataIndex] || WInput;
  let type: any = {};
  if (dataIndex === 'paramType') {
    type = JSON.parse(JSON.stringify(ParamType));
    if (record.key?.includes('-')) {
      delete type['对象'];
    }
    value = getLabel(ParamType, record.paramType);
  } else if (dataIndex === 'valueType') {
    type = JSON.parse(JSON.stringify(ValueType));
    value = getLabel(ValueType, record.valueType);
  } else if (dataIndex === 'optional') {
    value = !hasOptional || hidden ? '' : record.optional ? '可选' : '必选';
  } else if (dataIndex === 'dataValueOptions') {
    value = hasOptional ? '设置' : record.value;
  }

  function gethah() {
    if (hasOptional) {
      if (record?.editing) {
        if (hidden) {
          if (dataIndex === 'optional') {
            return null;
          }
          return (
            <Form.Item name={dataIndex} style={{ margin: 0 }}>
              <W onSave={onSaveValue} type={type} record={record} />
            </Form.Item>
          );
        } else {
          if (dataIndex === 'value') {
            if (record.paramType === ParamType.对象) {
              return null;
            }
            return (
              <Form.Item name={dataIndex} style={{ margin: 0 }}>
                <W onSave={onSaveValue} type={type} />
              </Form.Item>
            );
          } else {
            return (
              <Form.Item
                name={dataIndex}
                style={{ margin: 0 }}
                rules={[
                  { required: dataIndex !== 'value', message: `${title}必填` },
                ]}
              >
                <W onSave={onSaveValue} type={type} />
              </Form.Item>
            );
          }
        }
      } else {
        if (dataIndex === 'value' && record.paramType === ParamType.对象) {
          return null;
        }
        return value;
      }
    } else {
      if (record?.editing) {
        if (dataIndex === 'value') {
          return (
            <Form.Item name={dataIndex} style={{ margin: 0 }}>
              <WInput record={record} onSave={onSaveValue} type={type} />
            </Form.Item>
          );
        }
        return (
          <Form.Item
            name={dataIndex}
            style={{ margin: 0 }}
            rules={[{ required: true, message: `${title}必填` }]}
          >
            <W onSave={onSaveValue} type={type} />
          </Form.Item>
        );
      } else {
        return value;
      }
    }
  }

  return <td {...restProps}>{gethah()}</td>;
}
